<template>
  <div>
    <el-collapse v-model="activeNames">
      <el-collapse-item name="1">
        <template slot="title">
          <span class="collapseTitle">通用属性</span>
        </template>
        <el-form :model="form" label-width="45px" label-position="right" size="medium">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="X">
                <el-input-number v-model="form.x" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="Y">
                <el-input-number v-model="form.y" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="宽度">
                <el-input-number v-model="form.width" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="高度">
                <el-input-number v-model="form.height" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="角度">
                <el-input-number v-model="form.angle" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="zIndex">
                <el-input-number v-model="form.zIndex" size="mini" controls-position="right" @change="handleFormChange"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-collapse-item>

      <el-collapse-item name="2">
        <template slot="title">
          <span class="collapseTitle">节点样式</span>
        </template>
        <dynamic-attrs-panel ref="attrsPanel" @attrsChanged='handleAttrsChanged'></dynamic-attrs-panel>
      </el-collapse-item>

      <el-collapse-item name="3">
        <template slot="title">
          <span class="collapseTitle">节点自定义属性</span>
        </template>
        <el-form :model="form" label-width="80px" label-position="right">
          <el-form-item label="selector">
            <el-input v-model="form.selector" @change="handleFormChange"></el-input>
          </el-form-item>

          <el-form-item label="链接桩">
            <el-switch v-model="form.isPort" @change="handleFormChange"></el-switch>
          </el-form-item>

          <el-form-item v-for="(value, key) in nodeData" :key="key" :label="key">
            <el-input :value="value + ''" readonly></el-input>
          </el-form-item>
        </el-form>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
var _ = require('lodash')
import ConfigUtil from '@/utils/graph-utils/configUtil'
import ShapeUtil from '@/utils/graph-utils/shapeUtil'
import GraphUtil from '@/utils/graph-utils/graphUtil'
import DynamicAttrsPanel from '@/views/drawTool/dialog/component/dynamic-attrs-panel.vue'

export default {
  name: 'NodePropertyPanel',
  components: { DynamicAttrsPanel },
  data() {
    return {
      activeNames: ['1', '2', '3'],
      form: {
        // 通用属性
        x: '',
        y: '',
        width: '',
        height: '',
        angle: '',
        zIndex: '',

        // 自定义属性
        isPort: false,
        selector: ''
      },
      node: null,
      nodeData: {}
    }
  },
  computed: {
    isPolyNode() {
      const result = this.node && (this.node.shape === 'polygon' || this.node.shape === 'polyline')
      return result
    },

    isTextNode() {
      return this.node && (this.node.shape === 'text-block' || this.node.shape === 'simpleText')
    }
  },
  watch: {
    '$store.state.graphModule.refreshNodeProp': function() {
      this.refreshPanel(this.$store.state.graphModule.operateNode)
    },

    '$store.state.graphModule.operateNode': function(newVal) {
      if (newVal && newVal.isNode()) {
        this.refreshPanel(this.$store.state.graphModule.operateNode)
      }
    }
  },
  methods: {
    refreshPanel(node) {
      if (!node) {
        const graph = this.$store.state.graphModule.graph
        const selectedCells = graph.getSelectedCells()
        if (selectedCells.length === 0) {
          return
        }
        node = selectedCells[0]
      }
      this.node = node
      const position = node.position()
      const size = node.size()
      this.form.x = position.x
      this.form.y = position.y
      this.form.width = size.width
      this.form.height = size.height
      this.form.angle = node.getAngle()
      this.form.zIndex = node.zIndex

      // 样式属性
      this.setAttrsPanelInfo()

      // 自定义属性
      if (node.data) {
        this.form.selector = node.data.selector || ''
        this.form.isPort = node.data.isPort || false

        this.nodeData = node.getData()
      } else {
        this.form.selector = ''
        this.form.isPort = false
        this.nodeData = {}
      }
    },

    // 设置样式面板信息
    setAttrsPanelInfo() {
      const self = this
      const attrsObject = {}
      const attrs = self.node.getAttrs()
      const isCustomizeNode = ConfigUtil.isCustomizeNode(self.node.shape)
      _.forIn(attrs, function(attrValue, attrKey) {
        // 对于x6原生节点，不需要处理text对象的属性
        if (!isCustomizeNode && attrKey === 'text') {
          return
        }
        _.forIn(attrValue, function(value, key) {
          if (key.indexOf('ref') !== -1 && key !== 'refPoints') {
            return
          }
          const kebabKey = _.kebabCase(key)
          if (kebabKey === 'style') {
            if (_.isObject(value)) {
              _.forIn(value, function(styleValue, styleKey) {
                _.set(attrsObject, styleKey, styleValue)
              })
            } else if (_.isString(value)) {
              const objStyle = GraphUtil.formatStrStyle2ObjStyle(value)
              _.forIn(objStyle, function(value, key) {
                _.set(attrsObject, key, value)
              })
            }
          } else if (!_.hasIn(attrsObject, kebabKey)) {
            _.set(attrsObject, kebabKey, value)
          }
        })
      })
      this.$refs.attrsPanel.setPanelInfos(attrsObject)
    },

    handleFormChange() {
      this.node.resize(this.form.width, this.form.height)
      this.node.position(this.form.x, this.form.y)
      this.node.setZIndex(this.form.zIndex)
      this.node.rotate(this.form.angle)

      // 自定义属性
      if (!this.node.data) {
        this.node.data = {}
      }
      this.node.data.selector = this.form.selector
      this.node.data.isPort = this.form.isPort
    },

    handleAttrsChanged(changedAttrs) {
      const self = this
      _.forIn(changedAttrs, function(value, key) {
        const newKey = key.indexOf('ref') === -1 ? key : _.camelCase(key)
        ShapeUtil.setNodeAttr(self.node, newKey, value)
      })
    }
  }
}
</script>

<style scoped>
.el-input-number {
  width: 100px;
  line-height: 26px;
}

.el-form-item {
  margin-bottom: 2px;
}

.el-collapse {
  border: 3px solid #ebeef5;
  padding: 5px;
}

.collapseTitle {
  font-weight: bold;
  font-size: 1.2em;
}
</style>